//
//  MyViewController.m
//  LazyCat
//
//  Created by zhanshu on 15/9/7.
//  Copyright (c) 2015年 zhanshu. All rights reserved.
//

#import "MyViewController.h"
#import "MyHeaderView.h"
#import "MyTableViewCell.h"
#import "MyMessViewController.h"
#import "SetViewController.h"//设置
@interface MyViewController ()<UITableViewDataSource,UITableViewDelegate> {
    NSArray *titleArr;
    NSArray *imgArr;
    NSArray *placeArr;
}
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) MyHeaderView *headerView;
@property (nonatomic) BOOL doHaveNotReadMess;//是否有未读信息
@end

@implementation MyViewController
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    _doHaveNotReadMess = [AppData share].doLogin;
    [self resetUserMess];
    //获取用户信息
    [self getUserMess];
}
- (void)resetUserMess {
//    [NTNetUtil reLoginNum:[AppData share].loginPhone psw:[AppData share].loginPsw complate:^(BOOL sucess){
        if ([AppData share].doLogin) {
            _headerView.userCode.hidden = NO;
            _headerView.userName.text = [NSString stringWithFormat:@"%@\n%@",[AppData share].username,[AppData share].usertypestr];
            [_headerView.headerImg setImageWithURL:[NSURL URLWithString:[[AppData share].img safeString]] placeholderImage:[UIImage imageNamed:@"我的_r1_c4.png"]];
        }else {
            _headerView.userCode.hidden = YES;
            _headerView.userName.text = @"点击登录/注册";
            _headerView.headerImg.image = [UIImage imageNamed:@"我的_r1_c4.png"];
        }
//    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self initNav:@"我的" doBack:NO];
    
    self.view.backgroundColor = BackColor;
    self.automaticallyAdjustsScrollViewInsets = NO;
    titleArr = @[@[@"购物订单"],@[@"我的钱包"],@[@"我分享的会员",@"常用信息",@"系统公告",@"邀请好友",@"设置"]];
    imgArr = @[@[@"我的_r10_c3.png"],@[@"我的_r14_c3.png"],@[@"我的_r16_c1.png",@"我的_r18_c3.png",@"我的_r20_c3.png",@"我的_r22_c1.png",@"我的_r24_c2.png"]];
    placeArr = @[@[@"",@""],@[@"积分/现金"],@[@"",@"银行卡/常用地址",@"通知消息/主题消息",@"邀请即送大红包",@""]];
    
    _headerView = [[[NSBundle mainBundle] loadNibNamed:@"MyHeaderView" owner:self options:nil] firstObject];
    _tableView.tableHeaderView = _headerView;
    _tableView.backgroundColor = BackColor;
    
    [self resetUserMess];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(goLoginView) name:@"PopLoginView" object:nil];
//    _headerView.userName.text = @"13111111111\n皇冠会员";
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [[titleArr objectAtIndex:section] count];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return titleArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *Cell = @"cell";
    MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Cell];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"MyTableViewCell" owner:self options:nil] firstObject];
    }
    cell.imgView.image = [UIImage imageNamed:imgArr[indexPath.section][indexPath.row]];
    cell.titleLable.text = titleArr[indexPath.section][indexPath.row];
    cell.detailLable.text = placeArr[indexPath.section][indexPath.row];
    if (indexPath.row == 2 &&indexPath.section == 2) {
        cell.redPointL.hidden = !_doHaveNotReadMess;
    }else {
        cell.redPointL.hidden = YES;
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSString *url = @"";
    NSString *titleName = @"";
    if (![AppData share].doLogin) {
//        [self goLoginView];
        [[AppData share].baseTabVC goLoginView];
        return;
    }
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            //购物清单
            url = [NSString stringWithFormat:ShoppingOrderUrl,[AppData share].username,[AppData share].lng,[AppData share].lat,[Utils md5SignStr]];
            titleName = @"购物清单";
        }
//        else if (indexPath.row == 1) {
//            //其他订单
//            url = [NSString stringWithFormat:OtherOrder,[AppData share].username,[AppData share].lng,[AppData share].lat,[Utils md5SignStr]];
//            titleName = @"其他订单";
//        }
    }else if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            //我的钱包
            url = [NSString stringWithFormat:MyIncomeInfo,[AppData share].username,[AppData share].lng,[AppData share].lat,[Utils md5SignStr]];
            titleName = @"我的钱包";
        }
    }else if (indexPath.section == 2) {
        if (indexPath.row == 0) {
            //我分享的会员
            url = [NSString stringWithFormat:MyShareUserList,[AppData share].username,[AppData share].lng,[AppData share].lat,[Utils md5SignStr]];
            titleName = @"我分享的会员";
        }else if (indexPath.row == 1) {
            //常用的信息
            url = [NSString stringWithFormat:NormalMess,[AppData share].username,[AppData share].lng,[AppData share].lat,[Utils md5SignStr]];
            titleName = @"常用信息";
        }else if (indexPath.row == 2) {
            //系统公告
            url = [NSString stringWithFormat:SystemMess,[AppData share].username,[AppData share].lng,[AppData share].lat,[Utils md5SignStr]];
            titleName = @"系统公告";
        }else if (indexPath.row == 3) {
            //邀请好友
//            url = @"";
//            titleName = @"邀请好友";
            NSString *url = [NSString stringWithFormat:HelloFriend,[AppData share].username];
            [[ShareView share] showMess:ShareToFriend withImgUrl:@"" url:url];
            return;
        }else if (indexPath.row == 4) {
            //设置
            SetViewController *setView = [[SetViewController alloc] init];
            [self.navigationController pushViewController:setView animated:YES];
            return;
        }
    }
    
    //跳转到相应页面
    Html5ViewController *html5 = [[Html5ViewController alloc] init];
    html5.requestUrl = url;
    html5.titleStr = titleName;
    [self.navigationController pushViewController:html5 animated:YES];
}
#pragma mark - 获取个人信息
- (void)getUserMess {
    if (![AppData share].doLogin) {
        [_tableView reloadData];
        return;
    }
    NSString *url = [NSString stringWithFormat:GetUserMess,[AppData share].username,[Utils md5SignStr]];
    [NTNetUtil sendRequestWithUrl:url requestName:GetUserMess method:NTRequestMethodGET parameters:nil block:^(NTHttpRequest *networkRequest, NSError *error) {
        NSDictionary *dic = networkRequest.responseData;
        NSString *suc = dic[@"r"];
        if ([suc isEqualToString:@"T"]) {
            NSDictionary *detail = dic[@"d"];
            NSString *noReadCount = detail[@"numunrradmessage"];
            if ([noReadCount integerValue] == 0) {
                _doHaveNotReadMess = NO;
            }else {
                _doHaveNotReadMess = YES;
            }
            [AppData share].baseTabVC.redLable.hidden = !_doHaveNotReadMess;
            [_tableView reloadData];
        }else {
            [[MessShowView share] showWithMess:dic[@"m"]];
        }
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
